package com.douqu.game.core.config.challenge;

import com.douqu.game.core.config.common.GoodsData;
import com.douqu.game.core.factory.DataFactory;
import com.douqu.game.core.factory.LoadFactory;
import com.douqu.game.core.util.LoadUtils;
import com.douqu.game.core.util.LogUtils;
import com.douqu.game.core.util.CoreUtils;

import java.util.Arrays;


/**
* @author wangzhenfei
*         2017-08-08 16:09
*         副本关卡
*/
public class LevelConfig extends BattleInitConfig {

    public String dialogueGroup;

    public String dialogueStart;

    public String dialogueEnd;
    /** 图标 */
    public String icon;
    /** 地图 */
    public String map;
    /** 头像是否显示 */
    public int showAvatar;

    /** 奖励组 */
    public GoodsData[] passReward;
    /** 解锁关卡 */
    public int unlockLevel;
    /** 宝箱奖励(掉落组ID) */
    public int boxReward;

    /**
     * 评星条件
     */
    public int[] starChecks;

    public String pos;

    public int[] supportCardIds;

    public int cardParamId;

    public int taskId;

    /**
     * 副本ID
     */
    private int instanceId;

    @Override
    public boolean check()
    {
        if(passReward == null)
        {
            LogUtils.error("Level check passReward is null -> id:" + id + " name:" + name);
            return false;
        }
        if(starChecks == null)
        {
            LogUtils.error("Load Level starChecks is null! id:" + id);
            return false;
        }

        if(supportCardIds == null)
        {
            LogUtils.error("Load Level supportCardIds is null! id:" + id);
            return false;
        }

        if(cardParamId != 0 && DataFactory.getInstance().getGameObject(DataFactory.CARD_PARAM_KEY, cardParamId) == null){
            LogUtils.error("Load Level cardParamId is null! id:" + id);
            return false;
        }
        return super.check();
    }


    public boolean isSupportCardIn(int cardId){
        for(int id : supportCardIds){
            if(id == cardId){
                return true;
            }
        }
        return false;
    }




    @Override
    public boolean setVariable(String key, String value)
    {
        if("starChecks".equals(key))
        {
            starChecks = LoadUtils.loadIntArray(key, value);
            for(int sc : starChecks)
            {
                if(DataFactory.getInstance(LoadFactory.isTestLoad()).getGameObject(DataFactory.STAR_CHECK_KEY, sc) == null)
                {
                    LogUtils.error("Level setVariable Error -> starCheck : " + value + " id:" + id);
                    return false;
                }
            }
            return true;
        }
        else if("supportCardIds".equals(key)){
            if(CoreUtils.isErrorValue(value)){
                supportCardIds = new int[0];
                return true;
            }
            supportCardIds = LoadUtils.loadIntArray(key, value);
            for(int sc : supportCardIds)
            {
                if(DataFactory.getInstance(LoadFactory.isTestLoad()).getGameObject(DataFactory.CARD_KEY, sc) == null)
                {
                    LogUtils.error("Level setVariable Error -> supportCardIds : " + value + " id:" + id);
                    return false;
                }
            }
            return true;
        }
        else if("passReward".equals(key))
        {
            passReward = LoadUtils.loadGoodDataToArray(key, value);
            return LoadUtils.checkGoods(passReward, this.getClass(), key, id);
        }
        else if("boxReward".equals(key))
        {
            this.boxReward = Integer.parseInt(value);
//            if(DataFactory.getInstance(LoadFactory.isTestLoad()).getGameObject(DataFactory.DROP_GROUP_KEY, boxReward) == null)
//            {
//                LogUtils.error("Level setVariable Error -> boxReward : " + value + " id:" + id);
//                return false;
//            }
            return true;
        }
        else
            return super.setVariable(key, value);
    }


    public int getInstanceId() {
        return instanceId;
    }

    public void setInstanceId(int instanceId) {
        this.instanceId = instanceId;
    }

    @Override
    public String toString() {
        return "LevelConfig{" +
                "icon='" + icon + '\'' +
                ", map='" + map + '\'' +
                ", showAvatar=" + showAvatar +
                ", passReward=" + Arrays.toString(passReward) +
                ", unlockLevel=" + unlockLevel +
                ", boxReward=" + boxReward +
                ", instanceId=" + instanceId +
                ", pos='" + pos + '\'' +
                "} " + super.toString();
    }
}
